package com.ruoyi.web.controller.nongjt;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.nongjt.domain.dto.CertificateStatisticsDTO;
import com.ruoyi.nongjt.service.CertificateStatisticsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 合格证统计Controller
 * 
 * @author ruoyi
 * @date 2024-01-01
 */
@RestController
@RequestMapping("/nongjt/certificateStatistics")
public class CertificateStatisticsController extends BaseController
{
    @Autowired
    private CertificateStatisticsService certificateStatisticsService;

    /**
     * 获取合格证统计概览数据
     */
    @GetMapping("/overview")
    public AjaxResult getStatisticsOverview(
            @RequestParam(value = "districtCode", required = false) String districtCode,
            @RequestParam(value = "year", required = false) String year,
            @RequestParam(value = "month", required = false) String month)
    {
        // 设置默认值
        if (year == null || year.isEmpty()) {
            year = String.valueOf(java.time.LocalDate.now().getYear());
        }
        
        CertificateStatisticsDTO result = certificateStatisticsService.getStatisticsOverview(districtCode, year, month);
        return success(result);
    }

    /**
     * 获取合格证开具数量统计（按月）
     */
    @GetMapping("/monthly")
    public AjaxResult getMonthlyStatistics(
            @RequestParam(value = "districtCode", required = false) String districtCode,
            @RequestParam(value = "year", required = false) String year)
    {
        if (year == null || year.isEmpty()) {
            year = String.valueOf(java.time.LocalDate.now().getYear());
        }
        
        List<CertificateStatisticsDTO.MonthlyStatistics> result = certificateStatisticsService.getMonthlyStatistics(districtCode, year);
        return success(result);
    }

    /**
     * 获取主要农产品数量统计
     */
    @GetMapping("/products")
    public AjaxResult getProductStatistics(
            @RequestParam(value = "districtCode", required = false) String districtCode,
            @RequestParam(value = "year", required = false) String year,
            @RequestParam(value = "month", required = false) String month)
    {
        if (year == null || year.isEmpty()) {
            year = String.valueOf(java.time.LocalDate.now().getYear());
        }
        
        List<CertificateStatisticsDTO.ProductStatistics> result = certificateStatisticsService.getProductStatistics(districtCode, year, month);
        return success(result);
    }

    /**
     * 获取街道/镇合格证打印数量统计
     */
    @GetMapping("/streets")
    public AjaxResult getStreetStatistics(
            @RequestParam(value = "districtCode", required = false) String districtCode,
            @RequestParam(value = "year", required = false) String year)
    {
        if (year == null || year.isEmpty()) {
            year = String.valueOf(java.time.LocalDate.now().getYear());
        }
        
        List<CertificateStatisticsDTO.StreetStatistics> result = certificateStatisticsService.getStreetStatistics(districtCode, year);
        return success(result);
    }

    /**
     * 获取主体合格证开具数量排名
     */
    @GetMapping("/entities")
    public AjaxResult getEntityStatistics(
            @RequestParam(value = "districtCode", required = false) String districtCode,
            @RequestParam(value = "year", required = false) String year)
    {
        if (year == null || year.isEmpty()) {
            year = String.valueOf(java.time.LocalDate.now().getYear());
        }
        
        List<CertificateStatisticsDTO.EntityStatistics> result = certificateStatisticsService.getEntityStatistics(districtCode, year);
        return success(result);
    }

    /**
     * 获取街道/镇用户数量统计
     */
    @GetMapping("/streetUsers")
    public AjaxResult getStreetUserStatistics(
            @RequestParam(value = "districtCode", required = false) String districtCode,
            @RequestParam(value = "year", required = false) String year)
    {
        if (year == null || year.isEmpty()) {
            year = String.valueOf(java.time.LocalDate.now().getYear());
        }
        
        List<CertificateStatisticsDTO.StreetUserStatistics> result = certificateStatisticsService.getStreetUserStatistics(districtCode, year);
        return success(result);
    }

    /**
     * 获取每日合格证开具实时数据
     */
    @GetMapping("/daily")
    public AjaxResult getDailyStatistics(
            @RequestParam(value = "districtCode", required = false) String districtCode,
            @RequestParam(value = "year", required = false) String year,
            @RequestParam(value = "month", required = false) String month)
    {
        if (year == null || year.isEmpty()) {
            year = String.valueOf(java.time.LocalDate.now().getYear());
        }
        if (month == null || month.isEmpty()) {
            month = String.valueOf(java.time.LocalDate.now().getMonthValue());
        }
        
        List<CertificateStatisticsDTO.DailyStatistics> result = certificateStatisticsService.getDailyStatistics(districtCode, year, month);
        return success(result);
    }
}
